package nyist.media.controller;

import nyist.media.annotation.Log;
import nyist.media.bean.role.RoleInfoBean;
import nyist.media.page.ApiResult;
import nyist.media.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 角色控制器
 *
 * @author : dcf
 * @date : 2019/3/13
 */
@RequestMapping(value = "/admin/roles", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
@RestController
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Map<String, Object> list(Integer page, Integer limit) {
        return toResultPage(roleService.findPage(new PageRequest(page - 1, limit)));
    }

    @GetMapping("{id:\\d+}")
    public Map<String, Object> view(@PathVariable Long id) {
        return ApiResult.SUCCESS.getMap().add("result", roleService.getInfo(id));
    }

    @Log("更新角色")
    @PutMapping("{id:\\d+}")
    public Map<String, Object> update(@PathVariable Long id, @RequestBody RoleInfoBean param) {
        param.setId(id);
        roleService.update(param);
        return ApiResult.SUCCESS.getMap();
    }

    @Log("新增角色")
    @PostMapping
    public Map<String, Object> save(RoleInfoBean param) {
        roleService.save(param);
        return ApiResult.SUCCESS.getMap();
    }

    @Log("批量删除角色")
    @DeleteMapping
    public Map<String, Object> delete(Long[] ids) {
        roleService.deleteAll(ids);
        return ApiResult.SUCCESS.getMap();
    }
}

